"""视频预览组件"""
import cv2
import numpy as np
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QLabel
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtGui import QImage, QPixmap
from ..video_processor import SceneInfo

class VideoView(QWidget):
    """视频预览组件"""
    def __init__(self):
        super().__init__()
        self.cap = None
        self.current_frame = None
        self.play_timer = QTimer()
        self.play_timer.timeout.connect(self._update_frame)
        
        self._init_ui()
        
    def _init_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        
        # 创建视频显示标签
        self.video_label = QLabel()
        self.video_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.video_label.setMinimumSize(640, 360)
        self.video_label.setStyleSheet("background-color: black;")
        layout.addWidget(self.video_label)
        
    def load_video(self, video_path):
        """加载视频"""
        # 释放之前的视频
        self.release()
        
        # 打开新视频
        self.cap = cv2.VideoCapture(video_path)
        if not self.cap.isOpened():
            return False
            
        # 显示第一帧
        ret, frame = self.cap.read()
        if ret:
            self._show_frame(frame)
            
        return True
        
    def show_scene(self, video_path, scene):
        """显示指定场景"""
        if not self.cap or self.cap.get(cv2.CAP_PROP_POS_FRAMES) != scene.start_frame:
            # 重新打开视频
            self.release()
            self.cap = cv2.VideoCapture(video_path)
            if not self.cap.isOpened():
                return False
                
            # 设置到场景起始帧
            self.cap.set(cv2.CAP_PROP_POS_FRAMES, scene.start_frame)
            
        # 读取并显示帧
        ret, frame = self.cap.read()
        if ret:
            self._show_frame(frame)
            
        return True
        
    def _show_frame(self, frame):
        """显示帧"""
        # 转换颜色空间
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        
        # 调整大小以适应显示区域
        h, w = frame.shape[:2]
        label_size = self.video_label.size()
        scale = min(label_size.width() / w, label_size.height() / h)
        new_size = (int(w * scale), int(h * scale))
        frame = cv2.resize(frame, new_size)
        
        # 转换为QImage
        h, w = frame.shape[:2]
        bytes_per_line = 3 * w
        image = QImage(frame.data, w, h, bytes_per_line, QImage.Format.Format_RGB888)
        
        # 显示图像
        self.video_label.setPixmap(QPixmap.fromImage(image))
        self.current_frame = frame
        
    def _update_frame(self):
        """更新帧"""
        if not self.cap:
            return
            
        ret, frame = self.cap.read()
        if ret:
            self._show_frame(frame)
        else:
            self.play_timer.stop()
            
    def release(self):
        """释放资源"""
        if self.cap:
            self.cap.release()
            self.cap = None
        self.play_timer.stop()
        self.video_label.clear()
        self.current_frame = None
        
    def closeEvent(self, event):
        """窗口关闭事件"""
        self.release()
        super().closeEvent(event) 